<?php

include_once("../../inc/global.php");
include_once("../menusys/function.php");
include_once("../menusys/fun.php");
adminislogin();
$MenuID=$_GET["MenuID"];
$Superior=$_GET["Superior"];
if(empty($MenuID)||!is_numeric($MenuID)){
	YKT_error('参数不正确',$_SERVER['HTTP_REFERER']);
}

if(!isGetRule($MenuID,$_SESSION[ADMINSESSION][0],2) && strtolower($_SESSION[ADMINSESSION][1])!='admin'){
	YKT_error("对不起，你没有操作的权限",$_SERVER['HTTP_REFERER']);
}

$html->includetop();
$GetTableName=$System->GetTableName($MenuID);  //获取表名
$getfromconfig=$System->GetMenuSysFormConfig($MenuID);  //获取配置
$getfromconfigarr=explode(",",$getfromconfig);


//创建添加sql
function getfieldtype($TableName,$GetFileNum){
	$db=new mysql();
	$result=$db->query("select * From {$TableName}");
	return @mysql_field_type($result,$GetFileNum);
}


//把获取过来的数据存入数组中
$j=0;
$isupvalue=0;
for($p=0;$p<count($getfromconfigarr);$p++){
	$getfromconfiglist=explode("|",$getfromconfigarr[$p]);
	if(intval($getfromconfiglist[4])==0){
			if($getfromconfiglist[1]=="6"){  //是否为上传文件提交的数据
				
				$getupfile=upfilepic("FileName_{$p}");
				
				
				if(!is_array($getupfile)){
					$postarr[$j]["fieldvalue"]=$getupfile;
					
				}else{
					if(strval($getupfile[1])=="文件不存在"){
						$isupvalue=1;
					}else{
						//$base->alert($getupfile[1]);
						$postarr[$j]["fieldvalue"]=$_POST["FileName_{$p}_path"];
					}
				}
			}elseif($getfromconfiglist[1]=="4"){
				$postcheckvalue=$_POST["FileName_{$p}"];
				if(is_array($postcheckvalue))
				{
					$checkvalue="";
					for($c=0;$c<count($postcheckvalue);$c++){
						$checkvalue.=$postcheckvalue[$c];
						if($c<count($postcheckvalue)-1){
							$checkvalue.=",";
						}
					}
					$postarr[$j]["fieldvalue"]=$checkvalue;
				}else{
					$postarr[$j]["fieldvalue"]=$_POST["FileName_{$p}"];
				}
			}else{
				$postarr[$j]["fieldvalue"]=$_POST["FileName_{$p}"];
			}
			if($isupvalue==0){
				$postarr[$j]["fieldname"]=$GetTableName.$p;
				if(strtolower(getfieldtype($GetTableName,$p))=="int"){
					$postarr[$j]["type"]="int";
				}else{
					$postarr[$j]["type"]="string";
				}
			}
			$isupvalue=0;
			$j++;
			
			
	}
}

$addSql="insert into {$GetTableName}";
$addfield="(";
$addvalue="values(";
$dian=0;

for($i=0;$i<count($postarr);$i++){
	if($postarr[$i]["type"]=="int"){
		$addfield.=$postarr[$i]["fieldname"];
		$addvalue.=$postarr[$i]["fieldvalue"];
	}else{
		$addfield.=$postarr[$i]["fieldname"];
		$addvalue.="'{$postarr[$i]["fieldvalue"]}'";
	}
	if($i<(count($postarr)-1)){
		$addfield.=",";
		$addvalue.=",";
	}
}


$addfield.=",ClassSys2)";
$addvalue.=",{$Superior})";
$addSql.=$addfield.$addvalue;
if($db->query($addSql)){
	
	$getid=$db->getinsertid();
	$classpathvalue='';
	if(intval($Superior)==0){
		$classpathvalue=$getid;
	}elseif(intval($Superior)>0){
		$dbpath=$db->getoners('select classsys5 from classsys where classsys0='.$Superior.' order by classsys0 desc limit 1');
		$classpathvalue=$dbpath[0].','.$getid;
	}
	$updatasql='update classsys set classsys5=\''.$classpathvalue.'\' where classsys0='.$getid.' limit 1';
	if($db->query($updatasql)){
		YKT_error('添加成功','/'.ADMINROOT.'/System/classsys/index.php?MenuID='.$MenuID);
	}else{
		YKT_error('添加失败','/'.ADMINROOT.'/System/classsys/index.php?MenuID='.$MenuID);
	}
}else{
	YKT_error('添加失败','/'.ADMINROOT.'/System/classsys/index.php?MenuID='.$MenuID);
}
$html->includeend();

?>